#!/usr/bin/env python3
# -*-coding:utf-8 -*-
import os
import base64
from io import BytesIO
from faker import Faker
from my_app.create_photo.id_number import IdNumber, CalculateTime
from PIL import ImageFont, ImageDraw, Image


def write_image(sex, age, f, is_create_photo: bool, s_t, area):
    con_path = os.path.abspath(os.path.dirname(__file__)).replace("\\", "/") + "/config/"
    img = Image.open(con_path + "source.jpg")
    draw = ImageDraw.Draw(img)
    fnt = ImageFont.truetype(con_path + "simsun.ttf", 38)

    if f == "last":
        birthday = s_t.year_last_day(age)
        describe = f"{age}岁最后一天"
    elif f == "first":
        birthday = s_t.year_first_day(age)
        describe = f"{age}岁第一天"
    elif f == "ago":
        birthday = s_t.n_days_ago(age)
        describe = f"{age}天"
    else:
        raise "未知的格式"

    fake = Faker("zh_CN")
    name = fake.name()[:3]
    sex = 1 if sex == "男" else 2
    num = IdNumber.generate_id(sex=sex, birthday_num=birthday.replace("-", ""), area=area)  # 身份证号码
    address = IdNumber(num).get_area_name()  # 住址
    birthday = IdNumber(num).get_birthday()  # 生日
    sex = "男" if IdNumber(num).get_sex() == 1 else "女"  # 性别
    output = "{0:{6}<4}{1:{6}<2}{2:{6}<19}{3:{6}<10}{4:{6}<8}{5}".format(name, sex, num, birthday, describe, address, chr(12288))
    print(output)

    if is_create_photo:
        birthday = birthday.split("-")
        address = address + "升龙广场B栋9号"
        draw.text((160, 70), name, fill='black', font=fnt)
        draw.text((165, 140), sex, fill='black', font=fnt)
        draw.text((350, 140), "汉", fill='black', font=fnt)
        draw.text((165, 210), birthday[0], fill='black', font=fnt)
        draw.text((310, 210), birthday[1], fill='black', font=fnt)
        draw.text((380, 210), birthday[2], fill='black', font=fnt)
        draw.text((155, 280), address[:11], fill='black', font=fnt)
        # 根据地址字符串的长度手动在图片指定位置换行
        if len(address) > 11:
            draw.text((155, 320), address[11:22], fill='black', font=fnt)
            if len(address[11:]) > 11:
                draw.text((155, 360), address[22:33], fill='black', font=fnt)
                if len(address[22:]) > 11:
                    draw.text((155, 400), address[33:44], fill='black', font=fnt)
                    if len(address[33:]) > 11:
                        draw.text((155, 440), address[44:], fill='black', font=fnt)
        draw.text((290, 465), num, fill='black', font=fnt)
        # 得到字节流并转为base64
        img_buffer = BytesIO()
        img.save(img_buffer, format='JPEG')
        byte_data = img_buffer.getvalue()
        base64_bytes = base64.b64encode(byte_data)
        base64_str = base64_bytes.decode()

        return {"base64": f"data:image/jpeg;base64,{base64_str}", "infos": f"{name}  {sex}  {describe}", "filename": f"{sex}-{describe}-{name}",
                "outputs": {"num": num, "birthday": '-'.join(birthday), "address": address}}


if __name__ == '__main__':
    s_t_class = CalculateTime(t=0)
    ls = [
        (25, '女', 'first'),
    ]
    for i in ls:
        print(write_image(age=i[0], sex=i[1], f=i[2], is_create_photo=True, s_t=s_t_class, area=''))


